/*
 * FOF资产策略配置模块
 */
require('!style!css!../css/config.css');
var configHtml = require('!html!../html/config.html');
var common = require('./common.js');
var page = require('./page.js');
var configGraph = require('./configGraph.js');
var fund = require('./fund.js');
var configVue;
var config = {
    option: {},
    param: common.staticData.configOrCombinationParam,
    init: function (option) {
        this.option = $.extend(this.option, option);
        page.setPage(3);
        common.initNav(2);
        $('.step').hide();
        $('#config').html(configHtml).show();
        this.initModel();
        this.initCallback();
        common.getData({
            path: 'getAssetOrStrategy',
            callback: 'loadAssetOrStrategy',
            extra: '?productTypeId=' + config.option.productTypeId
        });
    },
    initCallback: function () {
        page.initSelectedParams(3);
        window['loadAssetOrStrategy'] = function (json) {
            var data = common.filterData(json, 'assetStrategyInfoList');
            if (data) {
                configVue = new Vue({
                    el: '#config',
                    data: {
                        assetOrStrategyStr: data[0].type == 1 ? '资产' : '策略',
                        assetList: data
                    },
                    methods: {
                        startCalculate: function () {
                            config.next();
                        }
                    }
                });
                $('.config-model').on('change', '.table-bordered input[type="radio"]', function () {
                    var val = $(this).val();
                    if (!val || val == 'on') {
                        if (typeof $(this).attr('data-point') != 'undefined') {
                            val = $(this).attr('data-point');
                        }
                    }
                    $(this).parent().parent().parent().next().find('input').val(val);
                });
                $('.config-model').on('change', '.table-bordered input[type="range"]', function () {
                    var val = $(this).val();
                    $(this).parent().find('input[type="text"]').val(val * 10);
                });
                $('.benchMark-select').on('change', function () {
                    if ($('.viewpoint-radio-0').size() > 1) {
                        var val = $(this).val();
                        config.initViewPoint();
                    }
                });
                $('.config-model').on('change', 'input[name="backLength"]', function () {
                    var model = $('input[name="modelSelect"]:checked').val();
                    if (model == 2) {
                        config.initViewPoint();
                    }
                });
            }
        };
        window['viewPointCalResult'] = function (json) {
            var data = common.filterData(json, 'result');
            if (data) {
                $.each(data, function (k, v) {
                    $('.viewpoint').eq(k).val(v.yield);
                    $('.viewpoint-radio-' + k).attr('data-point', v.yield);
                });
            }
        };
    },
    initViewPoint: function () {
        var benchmarkIdsArray = [];
        $('.benchMark-select').each(function () {
            benchmarkIdsArray.push($(this).val());
        });
        if (benchmarkIdsArray.length == 0) {
            return;
        }
        common.getData({
            path: 'viewPointCal',
            callback: 'viewPointCalResult',
            extra: '?benchmarkIds=' + benchmarkIdsArray.join(',') + '&traceYearLengthParam=' + $('input[name="backLength"]:checked').val()
        });
    },
    next: function () {
        console.log('startCalculate click...');
        var model = $('input[name="modelSelect"]:checked').val();
        if (!model) {
            alert('请选择模型');
            return false;
        }
        var isNull = false;
        var upDownParams = [];
        $('#config .strategy-list tr').each(function () {
            var up = $(this).find('input').eq(0).val();
            up = $.trim(up);
            var down = $(this).find('input').eq(1).val();
            down = $.trim(down);
            if (!up || !down) {
                isNull = true;
            }
            upDownParams.push({
                id: $(this).attr('data-id'),
                up: up,
                down: down,
                assetStrategyId: parseInt($(this).attr('data-assetStrategyId'), 10)
            });
        });
        if (isNull) {
            alert('请填写上限/下限值');
            return false;
        }
        var benchmarkParams = [];
        $('#config .asset-list').each(function () {
            var select = $(this).find('.config-select').val();
            if (!select) {
                isNull = true;
            }
            benchmarkParams.push({id: select});
        });
        if (isNull) {
            alert('请选择基准值');
            //return false;
        }
        var viewpointParam = [];
        var weightParam = [];
        var param = $('#config .config-model');
        if (model == 2) {
            param.find('.viewpoint').each(function () {
                var val = $(this).val();
                if (!val) {
                    isNull = true;
                }
                viewpointParam.push({
                    viewpoint: val / 100,
                    confidence: $(this).parent().parent().next().find('input[type="text"]').val() / 100
                });
            });
            if (isNull) {
                alert('请选择观点');
                return false;
            }
        } else if (model == 4) {
            var isMax = true;
            var weight = 0;
            param.find('input[type="text"]').each(function () {
                var val = $(this).val();
                if (!val) {
                    isNull = true;
                } else {
                    weight += parseFloat(val);
                }
                weightParam.push({
                    weight: val
                });
            });
            if (weight < 99 || weight > 100) {
                isMax = false;
            }
            if (isNull) {
                alert('请填写权重值');
                return false;
            }
            if (!isMax) {
                alert('权重值错误');
                return false;
            }
        }
        var paramJson = {};
        paramJson.calType = 1;
        paramJson.productTypeId = config.option.productTypeId;
        paramJson.risk = config.option.risk;
        paramJson.fund = config.option.fund;
        paramJson.modelId = model;
        var childParams = [];
        $('#config .model-param').each(function () {
            var child = $(this).find('input:checked').val();
            childParams.push({value: child});
        });
        paramJson.modelParams = childParams;
        paramJson.upDownParams = upDownParams;
        paramJson.benchmarkParams = benchmarkParams;
        paramJson.viewpointParams = viewpointParam;
        paramJson.weightParam = weightParam;
        console.log(JSON.stringify(paramJson));
        var testFund = common.getKey(window.location.href, 'testFund');
        if (testFund == 1) {
            fund.init();
        } else {
            configGraph.init(paramJson);
        }
    },
    getConfigParamHtml: function (data, extra) {
        $.extend(data, extra);
        var html = '';
        html += '<div class="model-param">';
        html += '   <p class="title">' + data.num + '、' + data.title + '</p>';
        var radioName = '';
        $.each(data.list, function (k, v) {
            radioName = v.name || radioName;
            html += '   <div class="radio">';
            html += '        <label>';
            html += '           <input type="radio" name="' + radioName + '" value="' + v.value + '" ' + (v.checked ? 'checked' : '') + '>' + v.text;
            html += '       </label>';
            html += '   </div>';
        });
        html += '</div>';
        return html;
    },
    initModel: function () {
        $('input[name="modelSelect"]').on('change', function () {
            var val = $(this).val();
            var configHtml = '';
            $('.strategy-list').show();
            if (val == 1) {
                configHtml += config.getConfigParamHtml(config.param.iteration, {num: 1});
                configHtml += config.getConfigParamHtml(config.param.backLength, {num: 2});
            } else if (val == 2) {
                configHtml += config.getConfigParamHtml(config.param.backLength, {num: 1});
                configHtml += $('#BL').html();
            } else if (val == 3) {
                configHtml += config.getConfigParamHtml(config.param.backLength, {num: 1});
            } else if (val == 4) {
                configHtml += $('#customWeight').html();
                $('.strategy-list').hide();
            }
            $('.config-model').html(configHtml);
            if (val == 4) {
                var percent = '';
                var size = configVue.assetList.length;
                if (size) {
                    if (100 % size == 0) {
                        percent = 100 / size;
                    } else {
                        percent = parseFloat('' + 100 / size).toFixed(2);
                    }
                }
                $('.config-model').find('input').val(percent);
            } else if (val == 2) {
                config.initViewPoint();
            }
        });
    }
};
for (var key in config) {
    if (key) {
        exports[key] = config[key];
    }
}